/**
 * 
 * useful for testing. you set the results you want it to return, and it obliges, using a real xhr call
 * to be sure that works too...
 * @author maulin
 */
dojo.provide("medryx.orm.tests.support.MockPersistenceService");


    dojo.declare("medryx.TestAssociationClass", null, {
        id: new medryx.orm.Identifier(),
        value: new medryx.orm.Property({
			readOnly: false,
			label:1
		}),
        sister: new medryx.orm.Association({
            associationClass: "medryx.TestAssociationClass"
        }),
        collection: new medryx.orm.Collection({
            associationClass: "medryx.TestAssociationClass"
        })
    });
    
    dojo.declare("medryx.TestEntityClass", null, {
        id: null,
        defaultLazyProperty: null,
        lazyReadonlyProperty: null,
        lazyPropertyWithConverter: null,
        eagerProperty: null,
        lazyCollection: null,
        eagerCollection: null,
        lazyAsoociation: null,
        lazyReadonlyAssociation: null,
        eagerAssociation: null
    });
    
	medryx.TestEntityClass.mapping = {
        id: {
            propertyType: medryx.orm.Identifier
        },
        defaultLazyProperty: {
            propertyType: medryx.orm.Property,
            attributes: {
                label: 2,
                readOnly:false
            }
        },
        lazyReadonlyProperty: {
            propertyType: medryx.orm.Property,
            attributes: {
                readOnly: true
            }
        },
        lazyPropertyWithConverter: {
            propertyType: medryx.orm.Property,
            attributes: {
                converter: this.bracketStringConverter
            }
        },
        eagerProperty: {
            propertyType: medryx.orm.Property,
            attributes: {
                lazy: false,
                label:1
            }
        },
        lazyCollection: {
            propertyType: medryx.orm.Collection,
            attributes: {
                associationClass: "medryx.TestAssociationClass"
            }
        },
        eagerCollection: {
            propertyType: medryx.orm.Collection,
            attributes: {
                lazy: false,
                associationClass: "medryx.TestAssociationClass"
            }
        },
        lazyCollectionWithConverter: {
            propertyType: medryx.orm.Collection,
            attributes: {
                converter: this.bracketStringConverter,
                associationClass: "medryx.TestAssociationClass"
            }
        },
        lazyAssociation: {
            propertyType: medryx.orm.Association,
            attributes: {
                associationClass: "medryx.TestAssociationClass"
            }
        },
        lazyReadonlyAssociation: {
            propertyType: medryx.orm.Association,
            attributes: {
                readOnly: true,
                associationClass: "medryx.TestAssociationClass"
            }
        },
        eagerAssociation: {
            propertyType: medryx.orm.Association,
            attributes: {
                lazy: false,
                associationClass: "medryx.TestAssociationClass"
            }
        }
    };

dojo.declare("MockPersistenceService", medryx.orm.PersistenceService, {
	    createFakeXhr:function(sync) {
			return dojo.xhrPost({
				url:dojo.moduleUrl("medryx", "config.js"),
				sync:sync ? true : false
			})
		},
	   
        //set a loadResults and you get that back here.
        load: function(entityClass, id, properties, sync){
			this.loadCallCount = this.loadCallCount || 0;
			this.lastLoaded = {className:entityClass, id:id, properties:properties};
            var deferred = this.createFakeXhr(sync);
			var results =  dojo.isArray(this.loadResults) ? this.loadResults[this.loadCallCount] : this.loadResults;
			deferred.addCallback(function() {
				return results;
			});
			this.loadCallCount++;
            return deferred;
        },
        
        //set fetchResults and get that back
        fetch: function(entityClass, args, sync) {
          console.debug("in fetch", entityClass, args);
		  this.fetchCallCount = this.fetchCallCount || 0;
		  this.lastFetch = {entityClass:entityClass, args:args, sync:sync};
           var deferred =  this.createFakeXhr(sync);
		   var results = dojo.isArray(this.fetchResults) ? this.fetchResults[this.fetchCallCount] : this.fetchResults;
		   deferred.addCallback(function() {
                return results;
            });
            this.fetchCallCount++;
            return deferred;    
        },
        
        //set saveSuccess and get that back
        save: function(className, id, properties){
			this.saveCallCount = this.saveCallCount || 0;
           console.debug("in save", className, id, properties);  
		   this.lastSaved = {className:className, id:id, properties:properties};
           var deferred = this.createFakeXhr();
		   var results = dojo.isArray(this.saveResults) ? this.saveResults[this.saveCallCount] : this.saveResults;
		   deferred.addCallback(function() {
                return results;
            });
			this.saveCallCount++;
            return deferred;       
        },
        
        //set deleteResults and get that back
        deleteItem: function(className, id){
			this.deleteCallCount = this.deleteCallCount || 0;
           console.debug("in delete", className, id);     
		   this.lastDeleted = {className:className, id:id};
            var deferred =  this.createFakeXhr();
            var results = dojo.isArray(this.deleteResults) ? this.deleteResults[this.deleteCallCount] : this.deleteResults;
			deferred.addCallback(function() {
                return results;
            });
			this.deleteCallCount++;
            return deferred; 
        }
    });
